/*
visqrt
Copyright (C) 2010  Bhupendra Aole

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/

package aole.math;

public class Vec3 {
	public float x, y, z;

	public static final Vec3 zero = new Vec3(0, 0, 0);
	public static final Vec3 xAxis = new Vec3(1, 0, 0);
	public static final Vec3 yAxis = new Vec3(0, 1, 0);
	public static final Vec3 zAxis = new Vec3(0, 0, 1);

	public Vec3() {
	}

	public Vec3(Vec3 v) {
		x = v.x;
		y = v.y;
		z = v.z;
	}

	public Vec3(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public Vec3(double x, double y, double z) {
		this.x = (float) x;
		this.y = (float) y;
		this.z = (float) z;
	}

	public void init () {
		x = y = z = 0f;
	}

	public void set (float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public void set (Vec3 v) {
		x = v.x;
		y = v.y;
		z = v.z;
	}

	public void limit (float min, float max) {
		if (x < min)
			x = min;
		if (y < min)
			y = min;
		if (z < min)
			z = min;

		if (x > max)
			x = max;
		if (y > max)
			y = max;
		if (z > max)
			z = max;
	}

	public float length () {
		return (float) Math.sqrt(x * x + y * y + z * z);
	}

	public float lengthSqr () {
		return (x * x + y * y + z * z);
	}

	public void normalize () {
		float len = (float) Math.sqrt(x * x + y * y + z * z);
		if (len != 0f) {
			x /= len;
			y /= len;
			z /= len;
		}
	}

	public void add (Vec3 v) {
		x += v.x;
		y += v.y;
		z += v.z;
	}

	public void subtract (Vec3 v) {
		x -= v.x;
		y -= v.y;
		z -= v.z;
	}

	public Vec3 getNormalized () {
		float len = length();
		if (len == 0f)
			return zero;
		return new Vec3(x / len, y / len, z / len);
	}

	public Vec3 getAdded (Vec3 v) {
		return new Vec3(x + v.x, y + v.y, z + v.z);
	}

	public Vec3 getSubtracted (Vec3 v) {
		return new Vec3(x - v.x, y - v.y, z - v.z);
	}

	public Vec3 getScaled (float sf) {
		return new Vec3(x * sf, y * sf, z * sf);
	}

	public Vec3 getDivided (float df) {
		return new Vec3(x / df, y / df, z / df);
	}

	public static float dot (Vec3 v1, Vec3 v2) {
		return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
	}

	public static Vec3 add (Vec3 v1, Vec3 v2) {
		return new Vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
	}

	public static Vec3 cross (Vec3 v1, Vec3 v2) {
		return new Vec3(v1.y * v2.z - v2.y * v1.z, v1.z * v2.x - v2.z * v1.x,
				v1.x * v2.y - v2.x * v1.y);
	}

	@Override
	public String toString () {
		return "<" + x + "," + y + "," + z + ">";
	}

	public void scale (float f) {
		x *= f;
		y *= f;
		z *= f;
	}

	public void add (float f) {
		x += f;
		y += f;
		z += f;
	}

	public void read (String s) {
		String c[] = s.split(",");
		x = Float.parseFloat(c[0]);
		y = Float.parseFloat(c[1]);
		z = Float.parseFloat(c[2]);
	}
}
